package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.PieDataEntity;
import com.wnhz.work.yz.bean.TreasureBean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;
import com.wnhz.work.yz.view.PieChart;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---财富宝
 */
public class TreasureActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    //    private RingView ring;
//    private MyChatView mychat;
    private PieChart pie;
    private TreasureBean bean;

    private TextView tv_asset;
    private TextView tv_may_money;
    private TextView tv_audit_money;
    private TextView tv_freeze_money;
    private TextView tv_today_get;
    private TextView tv_all_get;
    int[] pieColor = {0xff0abccc, 0xffe9573d, 0xff5271ef};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_treasure);

        initData();
        initView();
    }

    private void initData() {

    }

    private float[] pre = new float[3];
    private float[] pre2 = new float[3];
    private String strInfo[] = new String[]{"可提现金额", "提现审核中", "冻结金额"};

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("财富宝");
        pie = (PieChart) findViewById(R.id.pie);
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.rl_extract).setOnClickListener(this);
        findViewById(R.id.rl_bank_card).setOnClickListener(this);
        findViewById(R.id.rl_bill).setOnClickListener(this);
        findViewById(R.id.royalty).setOnClickListener(this);
//        ring = (RingView) findViewById(R.id.ring);
//        mychat = (MyChatView) findViewById(R.id.mychat);
        tv_asset = (TextView) findViewById(R.id.tv_asset);
        tv_may_money = (TextView) findViewById(R.id.tv_may_money);
        tv_audit_money = (TextView) findViewById(R.id.tv_audit_money);
        tv_freeze_money = (TextView) findViewById(R.id.tv_freeze_money);
        tv_all_get = (TextView) findViewById(R.id.tv_all_get);
        tv_today_get = (TextView) findViewById(R.id.tv_today_get);

        upload();
        upload2();

//        ring.setRotation(-90);
//        ring.setColors(new int[]{0xffe9573d, 0xff5271ef, 0xff0abccc});
//       ring.setValues(new int[]{9, 12, 11});
//        try {
//            ring.startDraw();
//        } catch (OnDrawingException e) {
//            e.printStackTrace();
//        }

    }

    private static final String TAG = "TreasureActivity";
    private int total;

    private void upload() {
        showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        XUtil.Post(Url.MONEY_MONEYDETAIL, map, new MyCallBack<String>() {
                    @Override
                    public void onSuccess(String result) {
                        closeDialog();
                        super.onSuccess(result);
                        Log.i(TAG, "onSuccess: TreasureActivity=  " + result);
                        try {
                            JSONObject jsonObject = new JSONObject(result);
                            String res = jsonObject.optString("result");
//                            if ("-1".equals(res)) {
//                                //info = "请重新登录";
//                                MyToast("请重新登录");
//                                startActivity(new Intent(TreasureActivity.this, LoginNewActivity.class));
//                                return;
//                            }
                            bean = new TreasureBean();
                            Gson gson = new Gson();
                            bean = gson.fromJson(jsonObject.optString("info"), TreasureBean.class);

                            tv_asset.setText(bean.getAllmoney());
                            tv_may_money.setText(bean.getWallet());
                            tv_audit_money.setText(bean.getTakingmoney());
                            tv_freeze_money.setText(bean.getFreezeingmoney());
                            tv_today_get.setText(bean.getTodaymoney());
                            tv_all_get.setText(bean.getAlltakemoney());
//                            ring.setValues(new int[]{9, 12, 11});
                            pre[0] = (float) Double.parseDouble(bean.getOrigin_wallet());
                            pre[1] = (float) Double.parseDouble(bean.getOrigin_takingmoney());
                            pre[2] = (float) Double.parseDouble(bean.getOrigin_freezeingmoney());

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onFinished() {
                        closeDialog();
                        for (int i = 0; i < pre.length; i++) {
                            total += pre[i];
                        }
                        if (total == 0) {
                            pre2[0] = 33.3f;
                            pre2[1] = 33.3f;
                            pre2[2] = 33.3f;
                        } else {
                            pre2[0] = pre[0] * 100 / total;
                            pre2[1] = pre[1] * 100 / total;
                            pre2[2] = pre[2] * 100 / total;
                        }
                        List<PieDataEntity> dataEntities = new ArrayList<>();
                        for (int i = 0; i < pieColor.length; i++) {
                            PieDataEntity entity = new PieDataEntity("name" + i, i + 1, pieColor[i]);
                            entity.setValue(pre2[i]);
                            dataEntities.add(entity);
                            Log.e("---1", pre[i] + "--" + pre2[i]);
                        }
                        pie.setDataList(dataEntities);


                        pie.setOnItemPieClickListener(new PieChart.OnItemPieClickListener() {
                            @Override
                            public void onClick(int position) {
                                Toast.makeText(TreasureActivity.this, strInfo[position] + "" + pre[position], Toast.LENGTH_SHORT).show();
                            }
                        });
                        super.onFinished();
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        closeDialog();
                        super.onError(ex, isOnCallback);
                    }
                }

        );
    }

    private void upload2() {
        showDialog();
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        XUtil.Post(Url.UCENTER_GETTODAYTICHENG, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                closeDialog();
                Log.e("----", result);
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                closeDialog();
                super.onError(ex, isOnCallback);
            }
        });
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //提现
            case R.id.rl_extract:
                intent = new Intent(this, WithdrawActivity.class);
                intent.putExtra("getwall",pre[0]+"");
                startActivity(intent);
                break;

            //银行卡
            case R.id.rl_bank_card:
                intent = new Intent(this, BankActivity.class);
                startActivity(intent);
                break;

            //账单
            case R.id.rl_bill:
                intent = new Intent(this, BillActivity.class);
                startActivity(intent);
                break;
            //提成
            case R.id.royalty:
                intent = new Intent(this, RoyaltyActivity.class);
                startActivity(intent);
                break;

            default:
                break;
        }
    }


}
